package com.pro.service.imple;

import com.pro.mapper.MicrocircleMapper;
import com.pro.pojo.*;
import com.pro.service.MicrocircleService;
import com.pro.utils.RedisCache;
import com.pro.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 谭金诚
 * @Description TODO
 * @Date 2022/1/10 10:57
 * @Version 1.0
 */
@Service
public class MicrocircleServiceImple implements MicrocircleService {

    @Autowired
    private MicrocircleMapper mapper;

    @Autowired
    RedisCache redisCache;

    @Override
    public List<JsLog> getLog(JsLog jsLog) {
        jsLog.setLogType(3);//logType:3（微圈日志）
        List<JsLog> log = mapper.getLog(jsLog);
        return log;
    }

    @Override
    public boolean delLog(Integer logId) {
        boolean flag = mapper.delLog(logId);
        return flag;
    }

    @Override
    public List<JsMicrocircle> getMyMicrocircleList(JsMicrocircle jsMicrocircle) {
        jsMicrocircle.setResult("1");//result:1（审核通过）
        jsMicrocircle.setType(4);//type:4（微圈）
        jsMicrocircle.setShow(2);//show:2（显示）
        List<JsMicrocircle> microcircleList = mapper.getMyMicrocircleList(jsMicrocircle);
        return microcircleList;
    }

    @Override
    public Integer getMyMicrocircleCollectionNums(Integer microcircleId) {
        Integer myMicrocircleCollectionNums = mapper.getMyMicrocircleCollectionNums(microcircleId,4);//collectionType:4（微圈）
        return myMicrocircleCollectionNums;
    }

    @Override
    public boolean delMicroCircle(Integer microcircleId) {
        boolean flag = mapper.delMicroCircle(microcircleId,3);//show:3（隐藏）
        return flag;
    }

    @Override
    public JsActicleCategroy queryMicroCircleClass(Integer acticleCategroyId) {
        JsActicleCategroy jsActicleCategroy = mapper.queryMicroCircleClass(acticleCategroyId);
        return jsActicleCategroy;
    }

    @Override
    public List<JsComment> getMicroCircleComment(Integer commodityId, Integer type) {
        //4是微圈
        List<JsComment> microCircleCommentAfter = mapper.getMicroCircleCommentAfter(commodityId,type);
        List<JsComment> microCircleCommentBefore = mapper.getMicroCircleCommentBefore(commodityId,type);
        microCircleCommentAfter.addAll(microCircleCommentBefore);
        //敏感词过滤
        List<JsComment> listFilter = SensitiveWordsUtil.getListFilter(microCircleCommentAfter);
        return listFilter;
    }

    @Override
    public boolean hideComment(Integer id, Integer display) {
        boolean flag = mapper.hideComment(id, display);
        return flag;
    }

    @Override
    public boolean delComment(Integer id, String status) {
        boolean flag = mapper.delComment(id, status);
        return flag;
    }

    @Override
    public boolean addMicroCircleComment(JsComment jsComment) {
        jsComment.setParentId(0);
        jsComment.setCommentDate(new Date());
        jsComment.setCreateDate(jsComment.getCommentDate());
        jsComment.setUpdateDate(jsComment.getCommentDate());
        jsComment.setType(4);//微圈
        jsComment.setDisplay(2);//显示
        jsComment.setFabulous(0);//点赞数量
        jsComment.setReply(0);//回复数量
        jsComment.setStatus("1");//正常
        jsComment.setUserType("2");//后台用户
        boolean flag = mapper.addMicroCircleComment(jsComment);
        return flag;
    }

    @Override
    public List<JsActicleCategroy> getJsActicleCategroy() {
        List<JsActicleCategroy> jsActicleCategroyRedis = redisCache.getCacheList("jsActicleCategroy");
        if (!jsActicleCategroyRedis.isEmpty()){
            return jsActicleCategroyRedis;
        }
        JsActicleCategroy jsActicleCategroy = new JsActicleCategroy();
        jsActicleCategroy.setActicleCategroyId(0);
        jsActicleCategroy.setArtcleCategroyName("选择分类类型");
        jsActicleCategroy.setArtcleCatefroyParentId(-1);
        List<JsActicleCategroy> jsActicleCategroyList = mapper.getJsActicleCategroy();
        jsActicleCategroyList.add(jsActicleCategroy);
        redisCache.setCacheList("jsActicleCategroy",jsActicleCategroyList);
        return jsActicleCategroyList;
    }

    //添加树节点
    @Override
    public boolean addZtree(JsActicleCategroy jsActicleCategroy) {
        jsActicleCategroy.setCreateDate(new Date());
        boolean flag = mapper.addZtree(jsActicleCategroy);
        JsActicleCategroy jsActicleCategroyTwo = new JsActicleCategroy();
        jsActicleCategroyTwo.setActicleCategroyId(0);
        jsActicleCategroyTwo.setArtcleCategroyName("选择分类类型");
        jsActicleCategroyTwo.setArtcleCatefroyParentId(-1);
        List<JsActicleCategroy> jsActicleCategroyList = mapper.getJsActicleCategroy();
        jsActicleCategroyList.add(jsActicleCategroyTwo);
        redisCache.deleteObject("jsActicleCategroy");
        redisCache.setCacheList("jsActicleCategroy",jsActicleCategroyList);
        List<JsActicleCategroy> jsActicleCategroyRedis = redisCache.getCacheList("jsActicleCategroy");
        return flag;
    }

    //删除树节点
    @Override
    public boolean delZtree(Integer acticleCategroyId) {
        boolean flag = mapper.delZtree(acticleCategroyId);
        JsActicleCategroy jsActicleCategroyTwo = new JsActicleCategroy();
        jsActicleCategroyTwo.setActicleCategroyId(0);
        jsActicleCategroyTwo.setArtcleCategroyName("选择分类类型");
        jsActicleCategroyTwo.setArtcleCatefroyParentId(-1);
        List<JsActicleCategroy> jsActicleCategroyList = mapper.getJsActicleCategroy();
        jsActicleCategroyList.add(jsActicleCategroyTwo);
        redisCache.deleteObject("jsActicleCategroy");
        redisCache.setCacheList("jsActicleCategroy",jsActicleCategroyList);
        List<JsActicleCategroy> jsActicleCategroyRedis = redisCache.getCacheList("jsActicleCategroy");
        return flag;
    }

    //修改树节点
    @Override
    public boolean updateZtree(String artcleCategroyName, Integer acticleCategroyId) {
        boolean flag = mapper.updateZtree(artcleCategroyName, acticleCategroyId);
        JsActicleCategroy jsActicleCategroyTwo = new JsActicleCategroy();
        jsActicleCategroyTwo.setActicleCategroyId(0);
        jsActicleCategroyTwo.setArtcleCategroyName("选择分类类型");
        jsActicleCategroyTwo.setArtcleCatefroyParentId(-1);
        List<JsActicleCategroy> jsActicleCategroyList = mapper.getJsActicleCategroy();
        jsActicleCategroyList.add(jsActicleCategroyTwo);
        redisCache.deleteObject("jsActicleCategroy");
        redisCache.setCacheList("jsActicleCategroy",jsActicleCategroyList);
        List<JsActicleCategroy> jsActicleCategroyRedis = redisCache.getCacheList("jsActicleCategroy");
        return flag;
    }

    //查询草稿状态微圈
    @Override
    public JsMicrocircle getDraftMicrocircle(Integer details) {
        JsMicrocircle draftMicrocircle = mapper.getDraftMicrocircle(details);
        return draftMicrocircle;
    }

    //修改草稿状态的微圈
    @Override
    public boolean updateDraftMicrocircle(JsMicrocircle jsMicrocircle) {
        boolean flag = mapper.updateDraftMicrocircle(jsMicrocircle);
        return flag;
    }

    //添加微圈
    @Override
    public boolean addDraftMirList(JsMicrocircle jsMicrocircle) {
        boolean flag = mapper.addDraftMirList(jsMicrocircle);
        return flag;
    }

    //保存后发布微圈
    @Transactional
    @Override
    public boolean addMirImg(JsMicrocircleImg jsMicrocircleImg) {
        jsMicrocircleImg.setCreateDate(new Date());
        jsMicrocircleImg.setUpdateDate(jsMicrocircleImg.getCreateDate());
        boolean flag = mapper.addMirImg(jsMicrocircleImg);
        if (flag){
            JsCourseRecord jsCourseRecord = new JsCourseRecord();
            jsCourseRecord.setCourseId(jsMicrocircleImg.getProductId());
            jsCourseRecord.setApplyDate(jsMicrocircleImg.getCreateDate());
            jsCourseRecord.setLaunchId(jsMicrocircleImg.getCreateId());
            jsCourseRecord.setResult(2);//待审核
            jsCourseRecord.setRemarks("微圈审核");
            jsCourseRecord.setType(4);//微圈
            boolean flagTwo = mapper.addMirExamine(jsCourseRecord);
            if (flagTwo){
                JsMicrocircle jsMicrocircle = new JsMicrocircle();
                jsMicrocircle.setReleaseDate(jsMicrocircleImg.getCreateDate());
                jsMicrocircle.setMicrocircleId(jsMicrocircleImg.getProductId());
                jsMicrocircle.setDetails(5);//微圈已提交
                boolean flagThree = mapper.upDraft(jsMicrocircle);
                return flagThree;
            }
        }
        return false;
    }

    //直接发布微圈
    @Transactional
    @Override
    public boolean addMicrocircle(JsMicrocircle jsMicrocircle) {
        boolean flag = mapper.addDraftMirList(jsMicrocircle);
        if (flag){
            JsMicrocircleImg jsMicrocircleImg = new JsMicrocircleImg();
            jsMicrocircleImg.setCreateDate(jsMicrocircle.getUpdateDate());
            jsMicrocircleImg.setProductId(jsMicrocircle.getMicrocircleId());
            jsMicrocircleImg.setMicrocircleImgUrl(jsMicrocircle.getArtcleUrl());
            jsMicrocircleImg.setMicrocircleImgType(3);
            jsMicrocircleImg.setCreateId(String.valueOf(jsMicrocircle.getUserId()));
            jsMicrocircleImg.setCreateDate(jsMicrocircle.getUpdateDate());
            jsMicrocircleImg.setUpdateId(String.valueOf(jsMicrocircle.getUserId()));
            jsMicrocircleImg.setUpdateDate(jsMicrocircle.getUpdateDate());
            boolean flagTwo = mapper.addMirImg(jsMicrocircleImg);
            if (flagTwo){
                JsCourseRecord jsCourseRecord = new JsCourseRecord();
                jsCourseRecord.setCourseId(jsMicrocircle.getMicrocircleId());
                jsCourseRecord.setApplyDate(jsMicrocircle.getUpdateDate());
                jsCourseRecord.setLaunchId(String.valueOf(jsMicrocircle.getUserId()));
                jsCourseRecord.setResult(2);//待审核
                jsCourseRecord.setRemarks("微圈审核");
                jsCourseRecord.setType(4);//微圈
                boolean flagThree = mapper.addMirExamine(jsCourseRecord);
                return flagThree;
            }
        }
        return false;
    }

    //添加微圈操作日志
    @Override
    public boolean addLog(JsLog jsLog) {
        jsLog.setCreateDate(new Date());
        boolean flag = mapper.addLog(jsLog);
        return flag;
    }

    //微圈统计
    @Override
    public List<JsMicrocircle> getStatistics() {
        List<Integer> list = mapper.getMicroCircleClassification();
        List<JsMicrocircle> hitsLikeNums = new ArrayList<>();
        for (Integer li : list){
            JsMicrocircle hitsLikeNums1 = mapper.getHitsLikeNums(li);
            Integer collectionNum = mapper.getCollectionNums(li);
            hitsLikeNums1.setCollectionNum(collectionNum);
            if (hitsLikeNums1.getActicleLike() == null){
                hitsLikeNums1.setActicleLike(0);
            }
            if (hitsLikeNums1.getActicleHits() == null){
                hitsLikeNums1.setActicleHits(0);
            }
            if (hitsLikeNums1.getCollectionNum() == null){
                hitsLikeNums1.setCollectionNum(0);
            }
            hitsLikeNums.add(hitsLikeNums1);
        }
        return hitsLikeNums;
    }
}
